package com.wrs.examsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wrs.examsystem.entity.Result.R;
import com.wrs.examsystem.entity.Test;
import com.wrs.examsystem.entity.Topic;
import com.wrs.examsystem.service.TestService;
import com.wrs.examsystem.util.wheel.TestUtil;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王荣升
 * @since 2024-05-15
 */
@RestController
@RequestMapping("/examsystem/test")
public class TestController {

    @Resource
    TestService testService;


    /**
     * 查询考试试卷内容，整理顺序
     * @param testId
     * @return
     */
    @GetMapping("findTest")
    public R findTest(@RequestParam("testId") Integer testId){
        if (testId <= 0){
            return R.error().message("试卷ID不合法");
        }
        return testService.findTest(testId);
    }

    @PostMapping("insertTests")
    public R insertTests(@RequestBody Test test){
        if (TestUtil.judgeEmpty(test)){
            return R.error();
        }
        return testService.insertTests(test);
    }
    /**
     * 查询部分考试信息概览
     * @param testIds ID表
     * @return
     */
    @PostMapping("findTests")
    public R findTests(@RequestParam("testIds[]") int[] testIds){
        if (testIds.length == 0){
            return R.error();
        }
//        Integer[] testId = new Integer[testIds.size()];
//        for (int i = 0; i < testIds.size(); i++) {
//            testId[i] = testIds.get(i);
//        }
        return testService.findTests(testIds);
    }

    /**
     * 根据学科查询试卷概览
     * @param subject
     * @return
     */
    @GetMapping("findBySubject")
    public R findBySubject(@RequestParam("subject") String subject){

        if (subject == null){
            return R.error();
        }
        return testService.findBySubject(subject);
    }

    /**
     * 删除试卷
     * @param testId 试卷ID
     * @return
     */
    @DeleteMapping("deleteTest")
    public R deleteTest(@RequestParam("testId") Integer testId){
        if(testId == null){
            return R.error();
        }else return testService.deleteTest(testId);
    }

    /**
     * 根据教师ID查询试卷
     * @param teacherId
     * @return
     */
    @GetMapping("findByTeacherId")
    public R findByTeacherId(@RequestParam("teacherId") Integer teacherId){
        if(teacherId == null) return R.error();
        QueryWrapper<Test> testQueryWrapper = new QueryWrapper<>();
        testQueryWrapper.eq("teacher_id",teacherId);
        List<Test> list = testService.list(testQueryWrapper);
        return R.ok().data(list);
    }

    /**
     * 根据教师ID查询做这份试卷的所有学生的成绩
     * @param teacherId
     * @return
     */
    @GetMapping("findStudentGrade")
    public R findStudentGrade(@RequestParam("teacherId") Integer teacherId){
        return testService.findStudentMark(teacherId);
    }
}

